package com.example.tuter.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.tuter.R;
import com.example.tuter.activity.NotificationActivity;
import com.example.tuter.activity.PostPublishActivity;
import com.example.tuter.activity.PostSearchActivity;
import com.example.tuter.activity.ViewPostActivity;
import com.example.tuter.activity.util.PageResultHandler;
import com.example.tuter.activity.util.PageResultHelper;
import com.example.tuter.adapter.PostAdapter;
import com.example.tuter.entity.Message;
import com.example.tuter.entity.PageResult;
import com.example.tuter.entity.Post;
import com.example.tuter.util.Constant;
import com.example.tuter.util.httpUtil.HttpFunction.CommunityAPI;

import java.util.ArrayList;

public class FragmentCommunity extends BaseFragment implements PageResultHandler<Post> {

    static final String TAG = FragmentCommunity.class.getSimpleName();





    private PageResultHelper<Post> pageResultHelper = new PageResultHelper<Post>();
    private PostAdapter postAdapter;
    private CommunityAPI communityAPI;
    private ImageView imageView_push;
    private TextView textView;
    private ImageView imageView_unread;
    private SwipeRefreshLayout swipeRefreshLayout;
    private FragmentActivity activity;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }



    @Override
    protected void initData() {
        communityAPI = Constant.r.create(CommunityAPI.class);
    }

    @Override
    protected void bindEvent() {
        //消息页面

        imageView_unread.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), NotificationActivity.class);
                startActivity(intent);
            }
        });

        //下拉刷新，清空内容，重新获取
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pageResultHelper.getFirstPageInRefreshing(FragmentCommunity.this, swipeRefreshLayout, postAdapter);
            }
        });


        //搜索跳转集成到首页来吧
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), PostSearchActivity.class);
                startActivity(intent);
            }
        });

        imageView_push.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), PostPublishActivity.class);
                startActivity(intent);
            }
        });

        postAdapter.setRecyclerItemClickListener(position -> {
            Intent intent = new Intent(getActivity(), ViewPostActivity.class);
            intent.putExtra("post", pageResultHelper.get(position));
            startActivity(intent);
        });
    }


    @Override
    protected int getLayoutID() {
        return R.layout.fragment_community;
    }


    @Override
    protected void initView(View view) {

        activity = getActivity();

        imageView_push = activity.findViewById(R.id.fab);


        //首页搜索框
        textView = activity.findViewById(R.id.home_search);
        imageView_unread = activity.findViewById(R.id.main_toolbar_metie);
        swipeRefreshLayout = activity.findViewById(R.id.main_refresh);
        RecyclerView recyclerView = activity.findViewById(R.id.main_recycleView);

        postAdapter = new PostAdapter(pageResultHelper.getData(), activity);
        recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        recyclerView.setAdapter(postAdapter);


        pageResultHelper.addPageScroll(this, recyclerView);


    }

    /**
     * 从本地的数据库中获取一定数据并填充至容器中先渲染
     */
    @Override
    public retrofit2.Call<Message<PageResult<Post>>> getDataByPage(Integer pageNo) {
        ArrayList<Post> posts = pageResultHelper.getData();
        if (pageNo == 0) {
            return communityAPI.getPost(null, null, Constant.PAGE_SIZE, Constant.POST_TYPE_NONE);
        }
        return communityAPI.getPost(posts.get(posts.size() - 1).getUpdateTime(), null, Constant.PAGE_SIZE, Constant.POST_TYPE_NONE);

    }

    @Override
    public void updateUI() {
        postAdapter.notifyItemRangeChanged(0, pageResultHelper.size());

    }


}
